package cn.study.luckyinfrastructure.gateway.iml;

import cm.study.config.enums.LdExeceptionEnum;
import cm.study.config.util.AssertUtil;
import cn.study.luckyclient.dto.query.RecordListByParamQuery;
import cn.study.luckydomain.gateway.RecordGateWay;
import cn.study.luckydomain.record.RecordEntity;
import cn.study.luckyinfrastructure.convertor.RecordConvertor;
import cn.study.luckyinfrastructure.gateway.iml.dataObject.RecordDB;
import cn.study.luckyinfrastructure.gateway.iml.mapper.RecordMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
public class RecordGateImpl implements RecordGateWay {
    private final RecordMapper recordMapper;
    @Override
    public RecordEntity save(RecordEntity entity) {
        RecordDB recordDB = RecordConvertor.toRecordDB(entity);
        AssertUtil.isTrue(recordMapper.insert(recordDB) != 1, LdExeceptionEnum.ADD_ERROR.getDescription());

        return RecordConvertor.toEntity(recordDB);
    }

    @Override
    public IPage<RecordEntity> page(RecordListByParamQuery query) {
        IPage<RecordDB> page=new Page<>();
        LambdaQueryWrapper<RecordDB> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(query.getRecordId()!=null,RecordDB::getId,query.getRecordId());
        lambdaQueryWrapper.eq(query.getWinTheLottery()!=null,RecordDB::getIsWinning,query.getWinTheLottery());
        lambdaQueryWrapper.eq(query.getStatus()!=null,RecordDB::getState,query.getStatus());
        lambdaQueryWrapper.eq(query.getActivityId()!=null,RecordDB::getActivityId,query.getActivityId());
        lambdaQueryWrapper.eq(query.getUserId()!=null,RecordDB::getUserId,query.getUserId());
        recordMapper.selectPage(page,lambdaQueryWrapper);
        return page.convert(RecordConvertor::toEntity);
    }

    @Override
    public Boolean updateStatus(Long id, Integer status) {
        return recordMapper.updateStatus(id,status);

    }
}